import java.util.ArrayList;
/**
 * This class represents the map that initializes all instances of cities, fields, and water
 * @author samkim
 *
 */

public class Map {
	private static ArrayList<Location> locationList;
	private GameSystem system;

	public ArrayList<Location> getLocationList() {
		return locationList;
	}

	public void setLocationList(ArrayList<Location> locationList) {
		this.locationList = locationList;
	}

	/**
	 * Initialization of the map. We should later change to this txt I/O
	 * @param system
	 */
	public Map(GameSystem system) {
		this.system = system;
		this.locationList = new ArrayList<Location>();
		
		// ----------------------Initialization -------------------------
		// We should have data in text file eventually
		Item k1 = new Item(Item.ItemType.k1);
		Item m16 = new Item(Item.ItemType.m16);
		
		//If for some reason, "Bremen" is no longer the starting location
		//then Location.isStartingLocation() must be modified!!
		City bremen = new City("Bremen");
		locationList.add(bremen);
		
		City oldenburg = new City("Oldenburg");
		locationList.add(oldenburg);
		
		City apeldoorm = new City("Apeldoorm");
		locationList.add(apeldoorm);
		
		City eindhoven = new City("Eindhoven");
		locationList.add(eindhoven);
		
		City bruxelles = new City("Bruxelles");
		locationList.add(bruxelles);
		
		City reims = new City("Reims");
		locationList.add(reims);
		
		City paris = new City("Paris");
		locationList.add(paris);
		
		City amiens = new City("Amiens");
		locationList.add(amiens);
		
		City lille = new City("Lille");
		locationList.add(lille);
		
		//Need for JUnit Test
		City fairyLand = new City("Fairy Land");
		locationList.add(fairyLand);
		
		bremen.setNext( new Fields(oldenburg, "Field of Bremen", 40, locationList) );
		bremen.getNext().setPrevious(bremen);
		
		oldenburg.setNext( new Fields(apeldoorm, "The Dark Forest", 60, locationList));
		oldenburg.getNext().setPrevious(oldenburg);
		
		apeldoorm.setNext( new Fields( eindhoven, "Holland", 60, locationList));
		apeldoorm.getNext().setPrevious(apeldoorm);
		
		eindhoven.setNext( new Fields( bruxelles, "Belgique Capital", 40, locationList));
		eindhoven.getNext().setPrevious(eindhoven);
		
		bruxelles.setNext( new Fields( reims, "Battlefield of France", 30, locationList));
		bruxelles.getNext().setPrevious(bruxelles);
		
		reims.setNext( new Fields( lille, "The Impossible Road", 80, locationList));
		reims.getNext().setPrevious(reims);
		
		reims.setRemField( new Fields( paris, "To the Lost Capital", 35, locationList));
		reims.getRemField().setPrevious(reims);
		
		paris.setNext( new Fields( amiens, "To the shore", 20, locationList));
		paris.getNext().setPrevious(paris);
		
		amiens.setNext( new Fields( lille, "Last Push", 40, locationList) );
		
		amiens.getNext().setPrevious(amiens);
		
		/*
		 * Initialize rivers manually since their location is fixed
		 */
		locationList.get(15).hasWater=true;
		locationList.get(61).hasWater=true;
		locationList.get(152).hasWater=true;
		locationList.get(200).hasWater=true;
		/*
		locationList.add(bremenField);
		locationList.add(oldenburgField);
		locationList.add(apeldoormField);
		locationList.add(eindhovenField);
		locationList.add(bruxellesField);
		locationList.add(reimsFieldL);
		locationList.add(reimsFieldP);
		locationList.add(parisField);
		locationList.add(amiensField);
		
		bremen.setFields(bremenField);
		oldenburg.setFields(oldenburgField);
		apeldoorm.setFields(apeldoormField);
		eindhoven.setFields(eindhovenField);
		bruxelles.setFields(bruxellesField);
		reims.setFields(reimsFieldL);
		reims.setRemField(reimsFieldP);
		paris.setFields(parisField);
		amiens.setFields(amiensField);
		*/
		
		//--------------------------Finish Initialization ----------------------------------------
		
	}
	
	public static Location locationSearch(String name, int length){
		for(int i=0; i<getLocations().size();i++)
		{
			if(getLocations().get(i).getName().equals(name)&&getLocations().get(i).getLength()==length)
				return getLocations().get(i);
		}
		return null;
	}
	/*
	private Location buildFieldStretch(Location start, Location end, String name, int length) {
		return
		
		
		if (length==0) {
			start.setNext(end);
			end.setPrevious(start);
		}
		else {
			start.setNext(buildFieldStretch(new Fields(name), end, name, --length) );
			start.getNext().setPrevious(start);
		}
		return start;
		
	}
	*/
   
  public static ArrayList<Location> getLocations() {
  
    return locationList;
  }

public ArrayList<Fields> getFields() {
	ArrayList<Fields> fields = new ArrayList<Fields>();
	for (Location loc:this.locationList)
		if (loc instanceof Fields)
			fields.add((Fields)loc);
	return fields;
}
	
	
}
